﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Exe1
{
    public partial class FrmQuery : Form
    {
        public Form1 frmPa;

        public FrmQuery()
        {
            InitializeComponent();
        }
        private bool CompString(string targetS, string queryS)
        {
            bool rt = false;

            // judge wether targetS contains queryS
            int pos = 0;
            int pLim = targetS.Length - queryS.Length;
            int i;
            bool bMatch = false;
            while (pos <= pLim)
            {
                if (targetS[pos]==queryS[0])
                {
                    bMatch = true;
                    for (i = 1; i < queryS.Length; i++ )
                    {
                        if (targetS[pos+i]!=queryS[i])
                        {
                            bMatch = false;
                            break;
                        }
                    }
                    if (bMatch)
                    {
                        rt = true;
                        break;
                    }
                    
                }
                pos++;
            }

            return rt;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string queryStr = tbxSearch.Text;
            string targetStr1;
            string targetStr2;
            string targetStr3;
            dataGridView1.Rows.Clear();
            int r;
            int idx = 0;
            for (r = 0; r < frmPa.m_vPapars.getCount(); r++)
            {
                targetStr1 = frmPa.m_vPapars.get(r).title;
                targetStr2 = frmPa.m_vPapars.get(r).authors;
                targetStr3 = frmPa.m_vPapars.get(r).zhaiyao;
                if (CompString(targetStr1, queryStr) || CompString(targetStr2, queryStr) || CompString(targetStr3, queryStr))
                {
                    // 新建一行
                    dataGridView1.Rows.Add();             
                    
                    // 填充当前行

                    DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
                    cell1.Value = frmPa.m_vPapars.get(r).title;
                    dataGridView1[0, idx++] = cell1;      
                }
            }
                       
        }        
        
    }
}
